package CutLoad.components
{
	import CutLoad.cookies.Fun;
	
	import flash.display.Sprite;
	import flash.events.FocusEvent;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	

	/**
	 * 文本输入框
	 * @author smallerbird
	 * 
	 */
	public class CTextInput extends CTextField
	{
		public var bak:Sprite;
		//文字的两种状态
		private var fmt1:TextFormat,fmt2:TextFormat
		//边框和背景的两种状态
		private var borderColor1:Number,backgroundColor1:Number,borderColor2:Number,backgroundColor2:Number
		//焦点获得，离开时调用的方法
		private var FocusEvent_out:Function=null;
		private var FocusEvent_in:Function=null;
		/**
		 * 返回 一个默认设置的CTextInput
		 * @return 
		 * 
		 */		
		public static function getSimple():CTextInput{
			var txt:CTextInput=new CTextInput();
			txt.init();
			return txt;
		}
		//
		public function CTextInput()
		{
		}
		/**
		 *  
		 * @param str 内容
		 * @param fmt TextFormat样式
		 * @param selectable
		 * 
		 */		
		override public function init(str:String="",fmt:TextFormat=null,selectable:Boolean=true):void{
			super.init(str,fmt,selectable);
			this.txt.type=TextFieldType.INPUT;
			this.txt.autoSize=TextFieldAutoSize.NONE;
			this.txt.border=true;
			this.txt.borderColor=0x0000ff;
			//
			this.txt.background=true;
			this.txt.backgroundColor=0xffffff;
			this.text=str;
			this.txt.height=20;
			//
			fmt1=this.txt.defaultTextFormat;
		}
		/**
		 * 初始化2
		 * @param maxChars 最大字数
		 * @param isPwd 是不是密码
		 * @return 
		 * 
		 */		
		public function init2(maxChars:Number=10,isPwd:Boolean=true):CTextInput{
			setIsPwd(isPwd)
			this.txt.maxChars=maxChars;
			return this
		}
		/**
		 * 设置是否自动适应宽度 
		 * 
		 */		
		public function setAuto():void{
			this.txt.autoSize=TextFieldAutoSize.LEFT;
		}
		/**
		 * 设置两种状态
		 * @param borderColor1 没有获得焦点时边框色
		 * @param backgroundColor1 没有获得焦点时背景色
		 * @param borderColor2 获得焦点时边框色
		 * @param backgroundColor2  获得焦点时背景色
		 * @param fmt2 获得焦点时TextFormat的状态
		 * 
		 */		
		public function setEventState(borderColor1:Number,backgroundColor1:Number,borderColor2:Number,backgroundColor2:Number,fmt2:TextFormat):void{
			this.fmt2=fmt2;
			this.borderColor1=borderColor1
			this.backgroundColor1=backgroundColor1
			this.borderColor2=borderColor2
			this.backgroundColor2=backgroundColor2;
			this.txt.addEventListener(FocusEvent.FOCUS_IN,focus_in);
			this.txt.addEventListener(FocusEvent.FOCUS_OUT,focus_out);
			set2State(true);
		}
		/**
		 *设置方本框可以输入最大字符数 
		 * @param numCount
		 * 
		 */		
		public function setMaxChars(numCount:Number):CTextInput{
			this.txt.maxChars=numCount
			return this;
		}
		/**
		 * 设置为密码
		 * @param isPwd
		 * 
		 */		
		public function setIsPwd(isPwd:Boolean=true):CTextInput{
			this.txt.displayAsPassword=	isPwd
				return this;
		}
		/**
		 * 设置文本框的两个事件 FocusEvent：in和out时，要执行的方法
		 * @param In
		 * @param out
		 * 
		 */		
		public function setFunFocusInOut(In:Function,Out:Function):void{
			FocusEvent_in=In;
			FocusEvent_out=Out;
		}
		private function focus_in(e:FocusEvent):void{
			if(FocusEvent_in!=null) FocusEvent_in();
			set2State(false)
		}
		private function focus_out(e:FocusEvent):void{
			if(FocusEvent_out!=null) FocusEvent_out();
			set2State(true)
		}
		private function set2State(isUp:Boolean=true):void{
			if(isUp){
				this.txt.borderColor=borderColor1;
				this.txt.backgroundColor=backgroundColor1;
				this.setTextFormat(fmt1);
			}else{
				this.txt.borderColor=borderColor2;
				this.txt.backgroundColor=backgroundColor2;
				this.setTextFormat(fmt2);
			}
			this.txt.text=this.txt.text;
		}
		//覆盖----------------------------------------
		/**
		 * 设置宽度,注：这里高设置无效
		 * @param w 宽度
		 * 
		 */		
		override public function setWH(w:Number=0,h:Number=20):void{
			this.txt.width=w;
			this.txt.height=h;
			//之前设为高设置无效
			//this.txt.height=this.txt.textHeight;
		}
		override public function getShowWidth():Number
		{
			return this.txt.width;
		}
		
		override public function getShowHeight():Number
		{
			return this.txt.height;
		}
	}
}